//
//  STUserInfoView.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/26.
//

import UIKit
import SDWebImage

class STLinearGradientView: UIView {
    
    var gradientLayer: CAGradientLayer!
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        gradientLayer = CAGradientLayer()
        gradientLayer.frame = self.bounds
        gradientLayer.startPoint = CGPoint.init(x: 0, y: 0.3)
        gradientLayer.endPoint = CGPoint.init(x: 1, y: 0.8545096516609192);
        gradientLayer.colors = [CGCOLOR_HEX("#FFA72C", 1), CGCOLOR_HEX("#FCBF1B", 1)]
        //将gradientLayer作为子layer添加到主layer上
        layer.addSublayer(gradientLayer)
        gradientLayer.cornerRadius = 10
        layer.cornerRadius = 10
        layer.borderWidth = 1
        layer.borderColor = UIColor.white.cgColor
        
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
        }
    }
    
    lazy var nameLabel: UILabel = {
        let lb = UILabel.init()
        lb.textColor = UIColor.white
        lb.text = "司机"
        lb.font = UIFont.systemFont(ofSize: 13)
        lb.textAlignment = .center
        return lb
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class STUserInfoView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(iconView)
        iconView.snp.makeConstraints { (make) in
            make.top.equalTo(0)
            make.left.equalTo(16)
            make.width.equalTo(68)
            make.height.equalTo(68)
        }
        
        addSubview(nameLabel)
        nameLabel.snp.makeConstraints { (make) in
            make.top.equalTo(iconView.snp.top).offset(5)
            make.left.equalTo(iconView.snp.right).offset(11)
            make.height.equalTo(30)
        }
        
        addSubview(identityView)
        identityView.snp.makeConstraints { (make) in
            make.centerY.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp.right).offset(8)
            make.height.equalTo(21)
            make.width.equalTo(44)
        }
        
        addSubview(addressLabel)
        addressLabel.snp.makeConstraints { (make) in
            make.bottom.equalTo(iconView.snp.bottom).offset(-5)
            make.left.equalTo(nameLabel)
            make.height.equalTo(22)
        }
        
        addSubview(sepLabel)
        sepLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(addressLabel)
            make.left.equalTo(addressLabel.snp.right).offset(8)
            make.height.equalTo(22)
        }
        
        addSubview(phoneLabel)
        phoneLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(addressLabel)
            make.left.equalTo(sepLabel.snp.right).offset(10)
            make.height.equalTo(22)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(changeTouxiangNotificationAction), name: Notification.Name("ChangeTouxiangNotificationName"), object: nil)
        
    }
    
    @objc func changeTouxiangNotificationAction (notifi : Notification) {
        let imgPath = notifi.userInfo?["filePath"]
        if(imgPath != nil) {
            let realUrl = URL.init(string: "\(kServiceHost)\(imgPath ?? "")")
            iconView.sd_setImage(with: realUrl, for: UIControl.State.normal, placeholderImage: UIImage.init(named: "icon_touxiang"), options: [.retryFailed])
        }
    }
    
    public func addTargetForIcon (_ target: Any?, action: Selector) {
        iconView.addTarget(target, action: action, for: .touchUpInside)
    }
    
    public func updateValue(_ data: Dictionary<String, Any>) {
        nameLabel.text = data["name"] as? String
        addressLabel.text = data["address"] as? String
        phoneLabel.text = data["driverTel"] as? String
        let avatar = data["avatar"] as? String
        if(avatar != nil) {
            let realUrl = URL.init(string: "\(kServiceHost)\(avatar ?? "")")
            iconView.sd_setImage(with: realUrl, for: UIControl.State.normal, placeholderImage: UIImage.init(named: "icon_touxiang"), options: [.retryFailed])
        }
    }
    
    lazy var iconView: UIButton = {
        let imgView = UIButton.init(type: .custom)
        imgView.setImage(UIImage.init(named: "icon_touxiang"), for: .normal)
        imgView.layer.cornerRadius = 33
        imgView.layer.masksToBounds = true
        return imgView
    }()
    
    lazy var nameLabel: UILabel = {
        let lb = UILabel.init()
        lb.textColor = UIColor.white
        return lb
    }()
    
    lazy var identityView: STLinearGradientView = {
        let imgView = STLinearGradientView.init(frame: CGRect.init(x: 0, y: 0, width: 44, height: 21))
        return imgView
    }()
    
    lazy var addressLabel: UILabel = {
        let lb = UILabel.init()
        lb.textColor = UIColor.white
        return lb
    }()
    
    lazy var sepLabel: UILabel = {
        let lb = UILabel.init()
        lb.textColor = UIColor.white
        lb.text = "|"
        return lb
    }()
    
    lazy var phoneLabel: UILabel = {
        let lb = UILabel.init()
        lb.textColor = UIColor.white
        return lb
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
